package com.yeyaomai.dksns.flowis.servlet;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.service.DownExcelService;
import com.yeyaomai.dksns.flowis.service.FlowisQueryService;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * 
 * @author dingtao@yeyaomai.com
 *
 * 下载
 */

@Controller
@RequestMapping("/downflowis/*")
public class DownFlowisController {

	private static Logger logger = LoggerFactory.getLogger(FlowisQueryDetailController.class);
	
	@Autowired
	FlowisQueryService flowisQueryService;
	
	@Autowired
	DownExcelService  downExcelService;
	
	@RequestMapping(value="down")
	public void download(HttpServletRequest request,HttpServletResponse response, FlowisQueryOrder order,
			ModelMap modelMap) {
		
	    response.setContentType("application/vnd.ms-excel");
//      单开一条线程  
//		ExecutorService  executorService  =Executors.newFixedThreadPool(2);
//		
//		executorService.submit(new Runnable(){@Override
//		public void run() {
//			   
//		}});
	    order.addSort("access_date", Order.DESCENDING.toString());
	    Page<FlowisStatDetailDTO>  result=	flowisQueryService.getSplit(order);
	    
	    List<String>  heads  =new ArrayList<String>();
	    heads.add("时间");
	    heads.add("IP");
	    heads.add("引擎");
	    heads.add("关键字");
	    heads.add("地理位置");
	    
	    
	    List<String> pros =new ArrayList<String>();
	    pros.add("access_date");
	    pros.add("ip");
	    pros.add("search_engine");
	    pros.add("key_word");
	    pros.add("access_area");
	    
	    
	    try{
	    downExcelService.exportExcel("最近来访("+VelocityToolDateUtils.getDay(0)+")", heads,result.getContent() , response.getOutputStream(), pros);
	    }catch(Exception e){
	    	logger.error(DownFlowisController.class.getName() +"下载错误------", e);
	    }
		
	}
	
	
}
